`
https://leetcode.cn/problems/convert-sorted-list-to-binary-search-tree/
`

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {ListNode} head
 * @return {TreeNode}
 */
var sortedListToBST = function (head) {
  // 把链表转化成数组，然后直接复用将有序数组转换为二叉搜索树的思路
  // https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/
  const arr = []
  while (head) {
    arr.push(head.val)
    head = head.next
  }

  const build = (nums, left, right) => {
    if (left > right) {
      return null
    }

    const mid = Math.floor((left + right) / 2)
    const root = new TreeNode(nums[mid])
    root.left = build(nums, left, mid - 1)
    root.right = build(nums, mid + 1, right)

    return root
  }

  return build(arr, 0, arr.length - 1)
};